package com.cyc.controller;

import com.alibaba.fastjson.JSON;
import com.cyc.entity.Comment;
import com.cyc.entity.News;
import com.cyc.entity.Product;
import com.cyc.service.CommentService;
import com.cyc.service.ProductService;
import com.cyc.util.RedisUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/comment")
public class CommentController {


    @Autowired
    ProductService productService;

    @Autowired
    RedisUtil redisUtil;

    @Autowired
    protected CommentService commentService;

    @RequestMapping("/all")
    public String selectAll(Integer pageNum,Model model){
        if (pageNum==null){
            pageNum=1;
        }
        PageHelper.startPage(pageNum,4);
        List<Comment> list=commentService.selectAll();
        PageInfo pageInfo=new PageInfo(list);

        List<Product> list1 = (List<Product>) redisUtil.get("productList");
        if (list1==null){
            list1 = productService.selectAll();
        }

        redisUtil.set("productList",list1);

        model.addAttribute("commentList",list);
        model.addAttribute("productList",list1);
        model.addAttribute("pageInfo",pageInfo);
        return "guestbook";
    }

    @RequestMapping("/manall")
    @ResponseBody
    public String manAll(Integer page,Integer limit){
        PageHelper.startPage(page,limit);
        List<Comment> list = commentService.selectAll();
        Map<String, Object> map=new HashMap<>();
        PageInfo pageInfo=new PageInfo(list);
        map.put("code",0);
        map.put("msg","成功");
        map.put("count",pageInfo.getTotal());
        map.put("data",list);
        return JSON.toJSONString(map);
    }

    @ResponseBody
    @RequestMapping("del")
    public String del(Integer id){
        Map<String, Object> map=new HashMap<>();
        int i = commentService.deleteByPrimaryKey(id);
        if (i>0){
            map.put("msg","删除成功");
        }else{
            map.put("msg","删除失败");
        }
        return JSON.toJSONString(map);
    }



    @RequestMapping("update")
    @ResponseBody
    public String update(Comment comment){
        Map<String, Object> map=new HashMap<>();
        comment.setIsdelete(2);
        comment.setCreatetime(new Date());
        int i = commentService.updateByPrimaryKey(comment);
        if (i>0){
            map.put("msg","修改成功");
        }else{
            map.put("msg","修改失败");
        }
        return JSON.toJSONString(map);
    }


    @RequestMapping("add")
    @ResponseBody
    public String add(Comment comment){
        Map<String, Object> map=new HashMap<>();
        comment.setCreatetime(new Date());
        comment.setIsdelete(2);
        int i = commentService.insert(comment);
        if (i>0){
            map.put("code",1);
            map.put("msg","提交留言成功");
        }else {
            map.put("code",0);
            map.put("msg","提交留言失败");
        }
        return JSON.toJSONString(map);
    }
}
